package com.liuyonggang.generator.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.liuyonggang.generator.model.ProjectInfo;
import com.liuyonggang.generator.service.ProjectInfoApi;
import com.liuyonggang.generator.util.PageInfo;
import com.liuyonggang.generator.util.ResultVo;

@Controller
@RequestMapping("/project")
public class ProjectInfoAction extends BaseAction{
    private static final Logger logger =LoggerFactory.getLogger(ProjectInfoAction.class);

    @Autowired
    private ProjectInfoApi projectInfoApi;
    
    @RequestMapping(value = {"", "/"})
    public String page(ModelMap map,@ModelAttribute PageInfo<ProjectInfo> pageInfo) {
        PageInfo<ProjectInfo> res = projectInfoApi.findAllByPage(pageInfo);
        map.put("page", res);
        return "/project/index";
    }
    
    @RequestMapping("/findCategory")
    @ResponseBody
    public List<ProjectInfo> findCategory() {
        List<ProjectInfo> list = projectInfoApi.findAll(null);
        return list;
    }
    
    @RequestMapping("/view")
    @ResponseBody
    public ProjectInfo view(@RequestParam(required=false)Integer id) {
        return projectInfoApi.findById(id);
    }
    
    @RequestMapping("/save/{action}")
    @ResponseBody
    public ResultVo save(HttpServletRequest request, HttpServletResponse response,@ModelAttribute ProjectInfo projectInfo, @PathVariable String action) {
       
        ResultVo rs = new ResultVo();
        if ("add".equals(action)) {
            rs = projectInfoApi.addProjectInfo(projectInfo);
        } else if ("update".equals(action)) {
            rs = projectInfoApi.updateProjectInfo(projectInfo);
        } else if ("delete".equals(action)) {
            rs = projectInfoApi.deleteProjectInfo(projectInfo);
        }  else {
            rs.setStatus(400);
            rs.setMessage("没有找到");
        }
        return rs;
    }
    
}
